package com.autonavi.collapse.task.model;

import java.util.HashMap;
import java.util.Map;

public class ExtendedMapIndex {

    private String value;
    private Map<String, ExtendedMapIndex> children = new HashMap<>();
    private Map<String, String> childrenValues = new HashMap<>();

    public ExtendedMapIndex() {
    }

    public ExtendedMapIndex(String key, String value) {
        if(key.isEmpty()){
            this.value = value;
        } else {
            int i = key.indexOf(".");
            if (i > 0) {
                String first = key.substring(0, i);
                String remain = key.substring(i + 1);
                children.put(first, new ExtendedMapIndex(remain, value));
            } else {
                children.put(key, new ExtendedMapIndex("", value));
            }
            childrenValues.put(key, value);
        }
    }

    public String getValue() {
        return value;
    }

    public void addIndex(String key, String value) {
        if (!key.isEmpty()) {
            int i = key.indexOf(".");
            if (i > 0) {
                String first = key.substring(0, i);
                String remain = key.substring(i + 1);
                ExtendedMapIndex childIndex = children.computeIfAbsent(first, k -> new ExtendedMapIndex(remain, value));
                childIndex.addIndex(remain, value);
            } else {
                ExtendedMapIndex childIndex = children.computeIfAbsent(key, k -> new ExtendedMapIndex("", value));
                childIndex.addIndex("", value);
            }
            childrenValues.put(key, value);
        }
    }

    public String getValue(String key) {
        if (!key.isEmpty()) {
            int i = key.indexOf(".");
            if (i > 0) {
                String first = key.substring(0, i);
                String remain = key.substring(i + 1);
                ExtendedMapIndex childIndex = children.get(first);
                if (childIndex != null) {
                    return childIndex.getValue(remain);
                }
                return null;
            }else {
                ExtendedMapIndex childIndex = children.get(key);
                if (childIndex != null) {
                    return childIndex.getValue();
                }
                return null;
            }
        } else {
            return value;
        }
    }

    public boolean isLeaf(){
        return children.isEmpty();
    }

    public ExtendedMapIndex getIndex(String key){
            int i = key.indexOf(".");
            if (i > 0) {
                String first = key.substring(0, i);
                String remain = key.substring(i + 1);
                ExtendedMapIndex extendedMapIndex = children.get(first);
                if (extendedMapIndex.isLeaf()) {
                    return extendedMapIndex;
                } else {
                    return extendedMapIndex.getIndex(remain);
                }
            }else {
                return children.get(key);
            }
    }

    public Map<String, String> getChildrenValues() {
        return childrenValues;
    }

    public String toString() {
        if (value == null) {
            return children.toString();
        }else {
            return value;
        }
    }
}
